<template>
  <div>
    <el-row :gutter="20">
      <el-col :span="7">
        <el-card>
          <el-form
            :rules="rules"
            :inline="true"
            ref="conncectFormRef"
            :model="connectForm"
            class="form-contain"
            size="small"
          >
            <el-form-item
              :label="t('strip.ip')"
              prop="ip"
            >
              <el-input
                v-model="connectForm.ip"
                :disabled="stripClientStatus"
                :placeholder="t('strip.serverIp')"
                @keyup.enter="connect(conncectFormRef)"
              />
            </el-form-item>
            <el-form-item
              :label="t('strip.port')"
              prop="port"
            >
              <el-input
                type="number"
                v-model.number="connectForm.port"
                :disabled="stripClientStatus"
                :placeholder="t('strip.port')"
                @keyup.enter="connect(conncectFormRef)"
              />
            </el-form-item>
            <el-form-item>
              <el-button
                :type="stripClientStatus ? 'danger' : 'primary'"
                @click="connect(conncectFormRef)"
                :loading="loading"
              >
                {{ t(stripClientStatus ? 'strip.disconnect' : 'strip.connect' ) }}
              </el-button>
            </el-form-item>
          </el-form>

          <!-- 根据消息类型展示不同的表单 -->
          <el-tabs
            type="border-card"
            v-model="activeName"
            class="demo-tabs"
            @tab-click="handleClick"
          >
            <el-tab-pane
              label="HB"
              name="HB"
            >
              <HbMessage
                :ip="connectForm.ip"
                :port="connectForm.port"
              />
            </el-tab-pane>
            <el-tab-pane
              label="Other"
              name="Other"
            >
              <CommonMessage
                :ip="connectForm.ip"
                :port="connectForm.port"
              />
            </el-tab-pane>
          </el-tabs>
        </el-card>
      </el-col>
      <el-col :span="17">
        <Log />
      </el-col>
    </el-row>
  </div>
</template>

<script lang="ts" setup>
import { FormInstance, FormRules, TabsPaneContext } from 'element-plus'
import { reactive, ref } from 'vue'
import CommonMessage from './components/Message.vue'
import HbMessage from './components/HbMessage.vue'
import Log from '@/components/Log/stripLog.vue'
import { closeClient, creatClient } from '@/api/strip'
import { useI18n } from 'vue-i18n'
import { stripStore } from '@/store/stripStore'
import { storeToRefs } from 'pinia'

const { t } = useI18n()

const activeName = ref('HB')

const loading = ref(false)

const store = stripStore()

const { stripClientStatus, stripClientIp, stripClientPort } = storeToRefs(store) // 连接状态

// 连接信息表单
const connectForm = reactive({
  ip: stripClientIp.value,
  port: stripClientPort.value
})

// 表单校验规则
const rules = reactive<FormRules>({
  ip: [
    { required: true, message: '请填写连接IP', trigger: 'blur' }
  ],
  port: [
    {
      required: true,
      message: '请填写连接端口',
      trigger: 'change'
    },
    { type: 'number', message: '端口号必须为数字类型' }
  ]
})

// 连接
const conncectFormRef = ref<FormInstance>()
const connect = async (formEl: FormInstance | undefined) => {
  // 储存ip和port，解决刷新丢失
  store.setStripClientIp(connectForm.ip)
  store.setStripClientPort(connectForm.port)

  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      loading.value = true

      if (stripClientStatus.value) {
        // 断开连接
        closeClient().then(res => {
          store.setStripClientStatus(false)
        }).finally(() => {
          loading.value = false
        })
      } else {
        // 建立连接
        creatClient(connectForm.ip, connectForm.port).then(res => {
          store.setStripClientStatus(true)
        }).finally(() => {
          loading.value = false
        })
      }
    }
  })
}

const handleClick = (tab: TabsPaneContext, event: Event) => {
  // console.log(tab, event)
}

</script>
<style scoped lang="scss">
.form-contain {
  margin-bottom: 20px;
}
</style>
